package com.ucode.dcse.option;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.ucode.springboot.starter.canal.option.dml.UpdateOption;

import org.springframework.stereotype.Component;
import java.util.List;

/**
 * 需要自己实现的更新处理机制
 * @author: liliang
 * @date: 2020年3月1日 下午10:16:03
 */
@Component
public class RealUpdateOption extends UpdateOption {
	
	@Override
	public void doOption(String destination, String schemaName, String tableName, CanalEntry.RowChange rowChange) {
		System.out.println("======================接口方式（更新数据操作）==========================");
		
		List<CanalEntry.RowData> rowDatasList = rowChange.getRowDatasList();
		for (CanalEntry.RowData rowData : rowDatasList) {
			String sql = "use " + schemaName + ";\n";
			StringBuffer updates = new StringBuffer();
			StringBuffer conditions = new StringBuffer();
			rowData.getAfterColumnsList().forEach((c) -> {
				if (c.getIsKey()) {
					conditions.append(c.getName() + "='" + c.getValue() + "'");
				} else {
					updates.append(c.getName() + "='" + c.getValue() + "',");
				}
			});
			sql += "UPDATE " + tableName + " SET " + updates.substring(0, updates.length() - 1) + " WHERE " + conditions;
			
			System.out.println(sql);
		}
		System.out.println( "\n======================================================");
		
	}
}